package com.e3.web.index.client;

import com.e3.web.core.client.GXTHelper;
import com.e3.web.core.client.SuperEntryPoint;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MainFrame extends SuperEntryPoint {
	
	private Widget northPanel;
	
	private Viewport contentPanel;
	
	private ContentPanel centerPanel;
	
	private LayoutContainer westPanel;

	private MainStatusBar statusBar;
	
	private MainWorkspace workspacePanel;
		
	private MainFrameServiceAsync service = MainFrameService.Util.getInstance();
	
	@Override
	protected void drawUI() {
		NavigatorHelper.setMainFrame(this);
		//north panel to show the corporate's icon and others.
		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.NORTH);
		bld.setSize(30);
		getContentPanel().add(getNorthPanel(),bld);
				
		//center panel to show the module's UI.
		BorderLayoutData ld = new BorderLayoutData(LayoutRegion.CENTER);
		getContentPanel().add(getCenterPanel(),ld);

		getCenterPanel().setBottomComponent(getStatusToolBar());
		String name = GXTHelper.getClientSession().getCorpName();
		if(name == null){
			name = GXTHelper.getClientSession().getCorpCode();
		}
		getStatusToolBar().setCompany(name);
		name = GXTHelper.getClientSession().getUserName();
		if(name == null){
			name = GXTHelper.getClientSession().getUserCode();
		}

		RootPanel root = RootPanel.get();
		root.add(getContentPanel());
		
	}
	private Widget getNorthPanel(){
		if(northPanel == null){
			northPanel = new HTMLPanel("<img alt=\"\" src=\"Resources/images/indextop.png\">");
		}
		return northPanel;
	}
	
	public Viewport getContentPanel(){
		if(contentPanel == null){
			contentPanel = new Viewport();
			contentPanel.setLayout(new BorderLayout());
		}
		return contentPanel;
	}
	
	public ContentPanel getCenterPanel(){
		if(centerPanel == null){
			centerPanel = new ContentPanel();
			
			centerPanel.setHeaderVisible(false);
			BorderLayout layout = new BorderLayout();
			centerPanel.setLayout(layout);
			
			BorderLayoutData bld = new BorderLayoutData(LayoutRegion.CENTER);
			centerPanel.add(getWorkspacePanel(), bld);
			
			ContentPanel cp = new ContentPanel(new FitLayout());
			cp.setHeading(MainMessages.INSTANCE.lab_westtitle());
			cp.add(getWestPanel());
			bld = new BorderLayoutData(LayoutRegion.WEST);
			bld.setCollapsible(true);
			bld.setSplit(true);
			bld.setSize(250);
			centerPanel.add(cp,bld);
		}
		return centerPanel;
	}
	
	public LayoutContainer getWestPanel(){
		if(westPanel==null){
			westPanel = new MainAccordion();
		}
		return westPanel;
	}
	
	public MainStatusBar getStatusToolBar(){
		if(statusBar==null){
			statusBar = new MainStatusBar();
		}
		return statusBar;
	}
	
	public MainWorkspace getWorkspacePanel(){
		if(workspacePanel == null){
			workspacePanel = new MainWorkspace(this);
		}
		return workspacePanel;
	}
	
	public MainFrameServiceAsync getService(){
		return service;
	}

	@Override
	protected LayoutContainer getUIComponent() {
		return null;
	}
	
}
